package com.application.lucky.widget.popupwindow;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.widget.time.TimeSelectorView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * create by liulp
 * on 2019-11-17 20:15
 */
public class PlayerQueryPop extends PopupWindow {
    private static final String TAG = "QueryPlayerDialog";
    private TextView tv_select_all;
    private RecyclerView mRclTimeView;
    private TextView mTvSure;
    private PlayerQueryPop.MyAdapter myAdapter;
    List<String> redps;
    private PlayerQueryPop.Select select;
    private TextView tv_start;
    private TextView tv_end;
    private Activity activity;
    private PopupWindow mPopupWindow;

    private PopupWindow startTimeSelectPop;
    private PopupWindow endTimeSelectPop;


    public PlayerQueryPop(Activity activity) {
        super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View rootView = LayoutInflater.from(activity).inflate(R.layout.dialog_player_query_view, null);
        setContentView(rootView);
        this.activity = activity;
        initView(rootView);
    }

    private void initView(View rootView) {
        redps = Arrays.asList(activity.getResources().getStringArray(R.array.select_time_long));

        mRclTimeView = rootView.findViewById(R.id.rcl_time_view);
        mRclTimeView.setLayoutManager(new GridLayoutManager(activity, 5));
        myAdapter = new PlayerQueryPop.MyAdapter(false);
        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                myAdapter.setSelectNum(position);
                String item = myAdapter.getItem(position);
                if (redps.contains(item)) {
                    if (select != null) {
                        select.select(item, -1);
                    }
                } else {
                    if (select != null) {
                        select.select(item, position);
                    }
                }
            }
        });
        mRclTimeView.setAdapter(myAdapter);
        myAdapter.setNewData(redps);

        tv_select_all = rootView.findViewById(R.id.tv_select_all);
        tv_select_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAdapter.setAllSelectNum();
            }
        });

        View layoutStartTime = rootView.findViewById(R.id.layout_start);
        layoutStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startTimeSelectPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
            }
        });

        layoutStartTime.postDelayed(new Runnable() {
            @Override
            public void run() {
                startTimeSelectPop = new FullTimeSelectPop(activity, new FullTimeSelectPop.SelectResultListener() {
                    @Override
                    public void onSelectResult(String time) {

                    }
                });

                View contentView = LayoutInflater.from(activity).inflate(R.layout.popu_timeselector, null);
                startTimeSelectPop = new PopupWindow(contentView,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                startTimeSelectPop.setFocusable(true);// 取得焦点
                //点击推出,要设置backgroundDrawable
                startTimeSelectPop.setBackgroundDrawable(new BitmapDrawable());
                /**设置PopupWindow弹出和退出时候的动画效果*/
                //      mPopupWindow.setAnimationStyle(R.style.animotorPdop);
                startTimeSelectPop.setOutsideTouchable(true);

                TimeSelectorView timeSelectorView = contentView.findViewById(R.id.timeselector);
                timeSelectorView.setListener(new TimeSelectorView.TimeChangeListener() {
                    @Override
                    public void scrollFinish(String time) {
                    }

                    @Override
                    public void onFinish(String time) {
                        tv_start.setText(time);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        }, 500L);

        View layoutEndTime = rootView.findViewById(R.id.layout_end);
        layoutEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                endTimeSelectPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
            }
        });
        layoutEndTime.postDelayed(new Runnable() {
            @Override
            public void run() {
                endTimeSelectPop = new FullTimeSelectPop(activity, new FullTimeSelectPop.SelectResultListener() {
                    @Override
                    public void onSelectResult(String time) {

                    }
                });

                View contentView = LayoutInflater.from(activity).inflate(R.layout.popu_timeselector, null);
                endTimeSelectPop = new PopupWindow(contentView,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                endTimeSelectPop.setFocusable(true);// 取得焦点
                //点击推出,要设置backgroundDrawable
                endTimeSelectPop.setBackgroundDrawable(new BitmapDrawable());
                /**设置PopupWindow弹出和退出时候的动画效果*/
                //      mPopupWindow.setAnimationStyle(R.style.animotorPdop);
                endTimeSelectPop.setOutsideTouchable(true);
                TimeSelectorView timeSelectorView = contentView.findViewById(R.id.timeselector);
                timeSelectorView.setListener(new TimeSelectorView.TimeChangeListener() {
                    @Override
                    public void scrollFinish(String time) {
                    }

                    @Override
                    public void onFinish(String time) {
                        tv_end.setText(time);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        }, 500L);


        tv_start = rootView.findViewById(R.id.tv_start_time);
        tv_end = rootView.findViewById(R.id.tv_end_time);

        mTvSure = rootView.findViewById(R.id.tv_sure);
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 传递数据
                dismiss();
            }
        });


    }

    private void showBirthdayDialog(final TextView selectTv, String birthdayText) {


    }

    public interface Select {
        void select(String s, int position);
    }

    static class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        List<String> selectNum = new ArrayList<>();

        public int getSelectNum() {
            return selectNum.size();
        }

        public void clearSelect() {
            selectNum.clear();
            notifyDataSetChanged();
        }

        private boolean isSelectNUm;

        public MyAdapter(boolean isSelect) {
            super(R.layout.item_group_send_rp_button);
            this.isSelectNUm = isSelect;
        }

        public boolean isSelect(int s) {
            return selectNum.contains(s + "");
        }

        public String getSelect() {
            Collections.sort(selectNum);
            StringBuffer sb = new StringBuffer();
            for (String s : selectNum) {
                sb.append(Integer.valueOf(s) + 1).append(",");

            }
            String s = sb.toString();
            String substring = s.substring(0, s.length() - 1);

            return substring;
        }

        public void setSelectNum(int position) {
            if (isSelectNUm) {
                if (selectNum.contains(position + "")) {
                    selectNum.remove(position + "");
                } else {
                    if (selectNum.size() == 9) {
                        return;
                    }
                    selectNum.add(position + "");
                }
                notifyItemChanged(position);
            } else {
                selectNum.clear();
                selectNum.add(position + "");
                notifyDataSetChanged();
            }
        }

        public void setAllSelectNum() {
            selectNum.clear();
            for (int position = 0; position < getItemCount(); position++) {
                selectNum.add(position + "");
            }
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            boolean contains = selectNum.contains(helper.getAdapterPosition() + "");
            helper.setText(R.id.tv_num_view, item).
                    setTextColor(R.id.tv_num_view, contains ? Color.parseColor("#ffffff") : Color.parseColor("#111111")).
                    setBackgroundRes(R.id.rl_view, contains ? R.drawable.shape_domain_text_cor_4 : R.drawable.shape_white_cor_4);
        }
    }
}
